//返回按钮
let back = document.querySelector(".back");

let token = localStorage.getItem("token");

// 获取结算区域
let orderSubmit = document.getElementById("orderSubmit");

// 获取收件人输入框
let receiver = document.getElementsByName("receiver")[0];

// 获取收获地址
let address = document.getElementsByName("address")[0];

let paymoney = document.querySelector(".paymoney span");
console.log(paymoney);

let box = document.querySelector(".box");

let host = "http://127.0.0.1:1337";

let user = document.querySelector(".order-head-right .user");
// 获取用户名
let uinfoUrl = host + "/api/v1/user/info";
// 头部设置token参数
axios.defaults.headers.common["token"] = token;
let uname = axios.get(uinfoUrl);

uname.then(function(res) {
    let userName = res.data.data.userName;
    user.innerHTML = userName;
})
// -----获取用户名结束

let cartUrl = host + "/api/v1/shop_carts";

// 设置头部信息
axios.defaults.headers.common["token"] = token;

async function re() {
  let carifo = await axios.get(cartUrl);
  let arr = carifo.data.data;
  console.log(carifo.data.data);
  let countprice = 0;
  let str = arr
    .map((item) => {
        if(item.product.coverImage.split("/")[1]=="uploads") {
            item.product.coverImage = host+ item.product.coverImage;
       } 
        console.log(item.product.coverImage);
      return `  
      <div class="ordermainbody">
         <div class="img">
              <img src="${item.product.coverImage}" alt="">
         </div>
          <div class="shopdel">
             <span>${item.product.name}</span>
             <span class="price" pri ="${item.product.price}">${item.product.price}元</span>
             <span class="count" cou ="${item.amount}">✖${item.amount}</span></div>
         </div> 
       </div>`;
    })
    .join("");
    

  box.innerHTML = str;
  let price = document.querySelectorAll(".price");
  let count = document.querySelectorAll(".count");
  for (let i = 0; i < price.length; i++) {
       let cou = Number(count[i].getAttribute("cou"));
       let pri = Number(price[i].getAttribute("pri"));
       console.log(cou);
       countprice += cou * pri;
  }
     console.log(countprice);
     paymoney.innerHTML = countprice;


  let products = [];
  arr.forEach(item=> {
      let obj = {product:item.product.id,amount:item.amount};
      products.push(obj);
  })
  console.log(products);

//提交订单
orderSubmit.onclick = async function() {
    let subData = {products,receiver:receiver.value,address:address.value};
    console.log(subData);
    if(receiver.value.trim()=="" || address.value.trim()=="") {
         alert("请输入用户名或者地址");
         //阻止程序继续运行
         return
    } 
    axios.defaults.headers.common["token"] = token;
    let subUrl = host + "/api/v1/orders";
    let subP1 = await axios.post(subUrl,subData);
    console.log(subP1);
    let receiverinfo = {receiver:receiver.value,address:address.value};
    // 将用户信息存入本地
    localStorage.setItem("receiverinfo",JSON.stringify(receiverinfo));
    receiver.value = "";
    address.value = "";
    location.href = "subsucces.html";
  }
}
re();


back.onclick = function() {
    location.href = "cars.html"
}
